package ru.payment.wm.impl;

import org.dom4j.Attribute;
import org.dom4j.Node;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 *
 * User: sergey
 * Date: 22.04.2009
 * Time: 17:25:43
 *
 */
public class X2OHelper {

  private SimpleDateFormat sdf = new SimpleDateFormat();

  public String selectNodeValueAsString(Node objNode, String path, String defValue) {
    Node node = objNode.selectSingleNode(path);
    return asString(node, defValue);
  }

  public int selectNodeValueAsInt(Node objNode, String path, int defValue) {
    Node node = objNode.selectSingleNode(path);
    return asInt(node, defValue);
  }

  public long selectNodeValueAsLong(Node objNode, String path, long defValue) {
    Node node = objNode.selectSingleNode(path);
    return asLong(node, defValue);
  }

  public float selectNodeValueAsFloat(Node objNode, String path, float defValue) {
    Node node = objNode.selectSingleNode(path);
    return asFloat(node, defValue);
  }

  public double selectNodeValueAsDouble(Node objNode, String path, double defValue) {
    Node node = objNode.selectSingleNode(path);
    return asDouble(node, defValue);
  }

  public Date selectNodeValueAsDate(Node objNode, String path, String pattern) {
    Node node = objNode.selectSingleNode(path);
    return asDate(node, pattern);
  }

  public String asString(Node node, String defValue) {
    return node!=null?node.getText():defValue;
  }

  public int asInt(Node node, int defValue) {
    return node!=null?Integer.parseInt(node.getText()):defValue;
  }

  public long asLong(Node node, long defValue) {
    return node!=null?Long.parseLong(node.getText()):defValue;
  }

  public float asFloat(Node node, float defValue) {
    return node!=null?Float.parseFloat(node.getText()):defValue;
  }

  public double asDouble(Node node, double defValue) {
    return node!=null?Double.parseDouble(node.getText()):defValue;
  }

  public boolean asBoolean(Node node, boolean defValue) {
    if (node == null) {
      return false;
    }
    String txt = node.getText();
    return "true".equalsIgnoreCase(txt)||"1".equals(txt);
  }

  public Date asDate(Node node, String pattern) {
    sdf.applyPattern(pattern);
    try {
      return node!=null?sdf.parse(node.getText()):null;
    } catch (ParseException e) {
      throw new RuntimeException(e);
    }
  }

  public String attrAsString(Attribute attr, String defValue) {
    return attr!=null?attr.getValue():defValue;
  }

  public int attrAsInt(Attribute attr, int defValue) {
    return attr!=null?Integer.parseInt(attr.getValue()):defValue;
  }

  public long attrAsLong(Attribute attr, long defValue) {
    return attr!=null?Long.parseLong(attr.getValue()):defValue;
  }

  public float attrAsFloat(Attribute attr, float defValue) {
    return attr!=null?Float.parseFloat(attr.getValue()):defValue;
  }

  public double attrAsDouble(Attribute attr, double defValue) {
    return attr!=null?Double.parseDouble(attr.getValue()):defValue;
  }

  public boolean attrAsBoolean(Attribute attr, boolean defValue) {
    if (attr == null) {
      return false;
    }
    String txt = attr.getValue();
    return "true".equalsIgnoreCase(txt)||"1".equals(txt);
  }

  public Date attrAsDate(Attribute attr, String pattern) {
    sdf.applyPattern(pattern);
    try {
      return attr!=null?sdf.parse(attr.getValue()):null;
    } catch (ParseException e) {
      throw new RuntimeException(e);
    }
  }

}
